<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center width="500px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140" class="grid grid-cols-1 gap-x-3">
      <el-form-item label="账号名称" prop="alipay_account" :rules="[{ required: true, message: '不能为空' }]">
        <el-input v-model="form.alipay_account" placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create()">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
// 表单处理
const formRef = ref()
const form = ref({})

const handleChange = ({ labels }) => {
  form.value.purchase_account = labels
}

// 打开弹窗
const open = async (aa_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  //新增
  form.value = {}
  loadings = reactive({})
  visible.value = true
  formRef?.value?.resetFields()
  if (aa_id) {
    const res = await api.detail({ aa_id })
    form.value = res.data
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async () => {
  if (!await formRef?.value.validate()) return
  loadings.create = true
  api[form.value.aa_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>